package cn.dengta.webapp.live.entity;

import java.util.Date;

import cn.dengta.common.model.Status;
import cn.dengta.common.util.Common;
import cn.dengta.webapp.base.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.Cmp;

/**
 * 直播用户信息表
 *
 * @author zw
 * @since 2022-12-26
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("im_live_user")
public class LiveUser extends BaseEntity {

	private static final long serialVersionUID = 1L;
	/** 只有进入直播间后待满 多少s 才算一个有效观众 */
	public static final int minValidJoinLife = 10;

	/** 直播id */
	Long liveId;
	/** 直播间主播用户id */
	Long anchorUserId;
	/** 用户id */
	Long userId;
	/** 用户等级 */
	Integer userLevel;
	/** 钻石消耗 */
	Integer coin;
	/** 创建时间（首次进入时间） */
	Date addTime;
	/** 离开时间 */
	Date endTime;
	/** 加入时间（最近一次） */
	Date joinTime;
	/** 观看时长（秒） */
	Integer duration;
	/** 观众在直播间的状态：2=直播结束；1=直播中；-1=主动退出；-2=被踢；-3=云信回调离开；-4=拉黑/被拉黑 */
	Integer status;
	/** 禁言截止时间：null=未被禁言；非null=禁言截止时间 */
	Date muteTime;

	public static LiveUser of(Live live, Long userId, Date now, Integer level) {
		final LiveUser liveUser = new LiveUser();
		liveUser.liveId = live.getId();
		liveUser.anchorUserId = live.getUserId();
		liveUser.userId = userId;
		liveUser.coin = 0;
		liveUser.duration = 0;
		liveUser.userLevel = level;
		liveUser.status = Status.YES.value;
		liveUser.setAddTime(now);
		liveUser.joinTime = now;
		return liveUser;
	}

	/** 是否封禁 */
	public boolean hasBan() {
		return Cmp.eq(this.status, -2);
	}

	/** 是否在直播间中 */
	public boolean hasLiving() {
		return Cmp.eq(this.status, 1);
	}

	/** 是否已经退出 */
	public boolean hasEnd() {
		return Cmp.eq(this.status, -1);
	}

	public String logKey() {
		return userId + Common.LINE_SEP + liveId;
	}

	public static final String LIVE_ID = "live_id";
	public static final String ANCHOR_USER_ID = "anchor_user_id";
	public static final String USER_ID = "user_id";
	public static final String USER_LEVEL = "user_level";
	public static final String COIN = "coin";
	public static final String ADD_TIME = "add_time";
	public static final String END_TIME = "end_time";
	public static final String STATUS = "status";
	public static final String DURATION = "duration";
	public static final String MUTE_TIME = "mute_time";

}
